
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class ThreadPool {
     private BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
     public void submit(Runnable runnable) throws InterruptedException {
         queue.put(runnable);
     }
     //创建线程池
     public ThreadPool(int n){
         for(int i = 0; i < n; i++){
             Thread t = new Thread(()-> {
                     try {
                         Runnable runnable = queue.take();
                         runnable.run();
                     } catch (InterruptedException e) {
                         throw new RuntimeException(e);
                     }
             });
             t.start();
         }
     }

    public static void main(String[] args) throws InterruptedException {
        ThreadPool threadPool = new ThreadPool(10);
        for(int i = 0; i < 10; i++){
            int id = i;
            threadPool.submit(() -> System.out.println("线程" + id));
        }
    }
}
